#ifndef VSqlQuery_H
#define VSqlQuery_H					 

#include "VSqlGenId.h"

///////////////////////////////////////////////////////////////////////////////

enum ESqlDbStatus
{
	ESqlDbStatus_actual  = 1,
	ESqlDbStatus_deleted = 1000
};

///////////////////////////////////////////////////////////////////////////////

typedef QList<QSqlField>  VSqlFieldList;
typedef QList<QSqlField*> VSqlFieldPtrList;
typedef QSharedPointer<QVariantList> VSqlSharedRecordPtr;

//
void VSqlQuery_fieldsToRecord(const VSqlFieldList &fieldList,VSqlSharedRecordPtr &record);
void VSqlQuery_fieldsPtrToRecord(const VSqlFieldPtrList &fieldPtrList,VSqlSharedRecordPtr &record);
void VSqlQuery_recordToFieldsPtr(const VSqlSharedRecordPtr &record,VSqlFieldPtrList &fieldPtrList);

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VSqlQuery_procedure
{
public:
	VSqlQuery_procedure(const QSqlDatabase& db);

	void prepare
	(
		const QString &sqlProcName,
		const VSqlFieldPtrList &sqlInParams
	);

	QString sql() const { return m_query.lastQuery();}

	void exec(const VSqlFieldPtrList &sqlInParams);
	bool next(VSqlFieldPtrList &sqlOutParams);

private:
	VSqlQuery m_query;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlQuery_insert
{
public:
	VSqlQuery_insert(const QSqlDatabase& db);

	void prepare
	(
		const QString &sqlTableName,
		const VSqlFieldList &sqlFieldsInsert
	);
	
	QString sql() const { return m_query.lastQuery();}

	void exec(const QSqlField &idField,VSqlFieldPtrList &sqlFieldsInsert);
	QVariant execEx(const QSqlField &idField,VSqlFieldList &sqlFieldsInsert);

private:
	VSqlGenId	m_genId;
	VSqlQuery m_query;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlQuery_select
{
public:
	VSqlQuery_select(const QSqlDatabase& db);

	void prepare
	(
		const QString &sqlTableName,
		const VSqlFieldList &sqlFieldsSelect,
		const QString &sqlWhere = QString(),
		const VSqlFieldList &sqlFieldsOrderBy = VSqlFieldList()
	);

	QString sql() const { return m_query.lastQuery();}
	
	void exec(const VSqlFieldList    &sqlFieldsWhere = VSqlFieldList());
	bool next(      VSqlFieldPtrList &sqlFieldsSelect);

private:
	VSqlQuery m_query;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlQuery_update
{
public:
	VSqlQuery_update(const QSqlDatabase& db);

	void prepare
	(
		const QString &sqlTableName,
		const VSqlFieldList &sqlFieldsUpdate,
		const QString &sqlWhere
	);

	QString sql() const { return m_query.lastQuery();}

	void exec
	(
		const VSqlFieldList &sqlFieldsUpdate,
		const VSqlFieldList &sqlFieldsWhere
	);

private:
	VSqlQuery m_query;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlQuery_delete
{
public:
	VSqlQuery_delete(const QSqlDatabase& db);

	void prepare
	(
		const QString &sqlTableName,
		const QString &sqlWhere
	);

	QString sql() const { return m_query.lastQuery();}

	void exec(const VSqlFieldList &sqlFieldsWhere);

private:
	VSqlQuery m_query;
};

///////////////////////////////////////////////////////////////////////////////

#endif
